#include "state_machine.h"

// 状态转换表
static state_transition_t state_table[] = {
    // IDLE 状态转换
    {STATE_IDLE, EVENT_START,  STATE_INIT,    handle_init_state},
    {STATE_IDLE, EVENT_EXIT,   STATE_EXIT,    handle_exit_state},
    
    // INIT 状态转换
    {STATE_INIT, EVENT_START,  STATE_RUNNING, handle_running_state},
    {STATE_INIT, EVENT_ERROR,  STATE_ERROR,   handle_error_state},
    {STATE_INIT, EVENT_EXIT,   STATE_EXIT,    handle_exit_state},
    
    // RUNNING 状态转换
    {STATE_RUNNING, EVENT_PAUSE,  STATE_PAUSE,   handle_pause_state},
    {STATE_RUNNING, EVENT_STOP,   STATE_IDLE,    handle_idle_state},
    {STATE_RUNNING, EVENT_ERROR,  STATE_ERROR,   handle_error_state},
    {STATE_RUNNING, EVENT_EXIT,   STATE_EXIT,    handle_exit_state},
    
    // PAUSE 状态转换
    {STATE_PAUSE, EVENT_RESUME, STATE_RUNNING, handle_running_state},
    {STATE_PAUSE, EVENT_STOP,   STATE_IDLE,    handle_idle_state},
    {STATE_PAUSE, EVENT_ERROR,  STATE_ERROR,   handle_error_state},
    {STATE_PAUSE, EVENT_EXIT,   STATE_EXIT,    handle_exit_state},
    
    // ERROR 状态转换
    {STATE_ERROR, EVENT_RESET,  STATE_IDLE,    handle_idle_state},
    {STATE_ERROR, EVENT_EXIT,   STATE_EXIT,    handle_exit_state},
};

// 获取状态转换表大小
#define STATE_TABLE_SIZE (sizeof(state_table) / sizeof(state_transition_t))

// 初始化状态机
int state_machine_init(state_machine_t *sm) {
    if (!sm) {
        printf("Error: Invalid state machine pointer\n");
        return -1;
    }
    
    sm->current_state = STATE_IDLE;
    sm->previous_state = STATE_IDLE;
    sm->initialized = 1;
    
    printf("State machine initialized, current state: %s\n", 
           state_to_string(sm->current_state));
    return 0;
}

// 处理事件
int state_machine_process_event(state_machine_t *sm, event_t event) {
    if (!sm || !sm->initialized) {
        printf("Error: State machine not initialized\n");
        return -1;
    }
    
    if (event >= EVENT_MAX) {
        printf("Error: Invalid event %d\n", event);
        return -1;
    }
    
    printf("Processing event: %s in state: %s\n", 
           event_to_string(event), state_to_string(sm->current_state));
    
    // 查找状态转换表
    for (int i = 0; i < STATE_TABLE_SIZE; i++) {
        if (state_table[i].current_state == sm->current_state && 
            state_table[i].event == event) {
            
            // 保存当前状态作为前一状态
            sm->previous_state = sm->current_state;
            
            // 调用状态处理函数
            if (state_table[i].handler) {
                state_t new_state = state_table[i].handler(sm, event);
                sm->current_state = new_state;
            } else {
                sm->current_state = state_table[i].next_state;
            }
            
            printf("State transition: %s -> %s\n", 
                   state_to_string(sm->previous_state), 
                   state_to_string(sm->current_state));
            return 0;
        }
    }
    
    printf("Warning: No transition found for event %s in state %s\n", 
           event_to_string(event), state_to_string(sm->current_state));
    return -1;
}

// 状态转字符串
const char* state_to_string(state_t state) {
    switch (state) {
        case STATE_IDLE:    return "IDLE";
        case STATE_INIT:    return "INIT";
        case STATE_RUNNING: return "RUNNING";
        case STATE_PAUSE:   return "PAUSE";
        case STATE_ERROR:   return "ERROR";
        case STATE_EXIT:    return "EXIT";
        default:            return "UNKNOWN";
    }
}

// 事件转字符串
const char* event_to_string(event_t event) {
    switch (event) {
        case EVENT_START:  return "START";
        case EVENT_STOP:   return "STOP";
        case EVENT_PAUSE:  return "PAUSE";
        case EVENT_RESUME: return "RESUME";
        case EVENT_ERROR:  return "ERROR";
        case EVENT_RESET:  return "RESET";
        case EVENT_EXIT:   return "EXIT";
        default:           return "UNKNOWN";
    }
}

// IDLE 状态处理函数
state_t handle_idle_state(state_machine_t *sm, event_t event) {
    printf("Handling IDLE state with event: %s\n", event_to_string(event));
    
    switch (event) {
        case EVENT_START:
            printf("System is starting initialization...\n");
            return STATE_INIT;
        case EVENT_EXIT:
            printf("System is exiting from IDLE state...\n");
            return STATE_EXIT;
        default:
            printf("Event %s not handled in IDLE state\n", event_to_string(event));
            return STATE_IDLE;
    }
}

// INIT 状态处理函数
state_t handle_init_state(state_machine_t *sm, event_t event) {
    printf("Handling INIT state with event: %s\n", event_to_string(event));
    
    switch (event) {
        case EVENT_START:
            printf("Initialization completed, starting system...\n");
            return STATE_RUNNING;
        case EVENT_ERROR:
            printf("Initialization failed, entering error state...\n");
            return STATE_ERROR;
        case EVENT_EXIT:
            printf("System is exiting from INIT state...\n");
            return STATE_EXIT;
        default:
            printf("Event %s not handled in INIT state\n", event_to_string(event));
            return STATE_INIT;
    }
}

// RUNNING 状态处理函数
state_t handle_running_state(state_machine_t *sm, event_t event) {
    printf("Handling RUNNING state with event: %s\n", event_to_string(event));
    
    switch (event) {
        case EVENT_PAUSE:
            printf("System is pausing...\n");
            return STATE_PAUSE;
        case EVENT_STOP:
            printf("System is stopping...\n");
            return STATE_IDLE;
        case EVENT_ERROR:
            printf("Error occurred during running, entering error state...\n");
            return STATE_ERROR;
        case EVENT_EXIT:
            printf("System is exiting from RUNNING state...\n");
            return STATE_EXIT;
        default:
            printf("Event %s not handled in RUNNING state\n", event_to_string(event));
            return STATE_RUNNING;
    }
}

// PAUSE 状态处理函数
state_t handle_pause_state(state_machine_t *sm, event_t event) {
    printf("Handling PAUSE state with event: %s\n", event_to_string(event));
    
    switch (event) {
        case EVENT_RESUME:
            printf("System is resuming...\n");
            return STATE_RUNNING;
        case EVENT_STOP:
            printf("System is stopping from pause...\n");
            return STATE_IDLE;
        case EVENT_ERROR:
            printf("Error occurred during pause, entering error state...\n");
            return STATE_ERROR;
        case EVENT_EXIT:
            printf("System is exiting from PAUSE state...\n");
            return STATE_EXIT;
        default:
            printf("Event %s not handled in PAUSE state\n", event_to_string(event));
            return STATE_PAUSE;
    }
}

// ERROR 状态处理函数
state_t handle_error_state(state_machine_t *sm, event_t event) {
    printf("Handling ERROR state with event: %s\n", event_to_string(event));
    
    switch (event) {
        case EVENT_RESET:
            printf("System is resetting from error state...\n");
            return STATE_IDLE;
        case EVENT_EXIT:
            printf("System is exiting from ERROR state...\n");
            return STATE_EXIT;
        default:
            printf("Event %s not handled in ERROR state\n", event_to_string(event));
            return STATE_ERROR;
    }
}

// EXIT 状态处理函数
state_t handle_exit_state(state_machine_t *sm, event_t event) {
    printf("Handling EXIT state with event: %s\n", event_to_string(event));
    printf("System is shutting down...\n");
    return STATE_EXIT;
}